home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / binutils / objdump.c < prev    next >
C/C++ Source or Header  |  1994-09-28  |  38KB  |  1,603 lines

  1. /* objdump.c -- dump information about an object file.
  2.    Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Binutils.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "bfd.h"
  21. #include "sysdep.h"
  22. #include "getopt.h"
  23. #include "bucomm.h"
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include "dis-asm.h"
  27. #include "libiberty.h"
  28.  
  29. /* Internal headers for the ELF .stab-dump code - sorry.  */
  30. #define    BYTES_IN_WORD    32
  31. #include "aout/aout64.h"
  32.  
  33. #ifndef FPRINTF_ALREADY_DECLARED
  34. extern int fprintf PARAMS ((FILE *, CONST char *, ...));
  35. #endif
  36.  
  37. char *default_target = NULL;    /* default at runtime */
  38.  
  39. extern char *program_version;
  40.  
  41. int show_version = 0;        /* show the version number */
  42. int dump_section_contents;    /* -s */
  43. int dump_section_headers;    /* -h */
  44. boolean dump_file_header;    /* -f */
  45. int dump_symtab;        /* -t */
  46. int dump_dynamic_symtab;    /* -T */
  47. int dump_reloc_info;        /* -r */
  48. int dump_dynamic_reloc_info;    /* -R */
  49. int dump_ar_hdrs;        /* -a */
  50. int with_line_numbers;        /* -l */
  51. int dump_stab_section_info;    /* --stabs */
  52. boolean disassemble;        /* -d */
  53. boolean disassemble_all;    /* -D */
  54. boolean formats_info;        /* -i */
  55. char *only;            /* -j secname */
  56.  
  57. /* Extra info to pass to the disassembler address printing function.  */
  58. struct objdump_disasm_info {
  59.   bfd *abfd;
  60.   asection *sec;
  61.   boolean require_sec;
  62. };
  63.  
  64. /* Architecture to disassemble for, or default if NULL.  */
  65. char *machine = (char *) NULL;
  66.  
  67. /* The symbol table.  */
  68. asymbol **syms;
  69.  
  70. /* Number of symbols in `syms'.  */
  71. long symcount = 0;
  72.  
  73. /* The sorted symbol table.  */
  74. asymbol **sorted_syms;
  75.  
  76. /* Number of symbols in `sorted_syms'.  */
  77. long sorted_symcount = 0;
  78.  
  79. /* The dynamic symbol table.  */
  80. asymbol **dynsyms;
  81.  
  82. /* Number of symbols in `dynsyms'.  */
  83. long dynsymcount = 0;
  84.  
  85. /* Forward declarations.  */
  86.  
  87. static void
  88. display_file PARAMS ((char *filename, char *target));
  89.  
  90. static void
  91. dump_data PARAMS ((bfd *abfd));
  92.  
  93. static void
  94. dump_relocs PARAMS ((bfd *abfd));
  95.  
  96. static void
  97. dump_dynamic_relocs PARAMS ((bfd * abfd));
  98.  
  99. static void
  100. dump_reloc_set PARAMS ((bfd *, arelent **, long));
  101.  
  102. static void
  103. dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
  104.  
  105. static void
  106. display_bfd PARAMS ((bfd *abfd));
  107.  
  108. static void
  109. objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
  110.  
  111. void
  112. usage (stream, status)
  113.      FILE *stream;
  114.      int status;
  115. {
  116.   fprintf (stream, "\
  117. Usage: %s [-ahifdDrRtTxsl] [-b bfdname] [-m machine] [-j section-name]\n\
  118.        [--archive-headers] [--target=bfdname] [--disassemble]\n\
  119.        [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
  120.        [--info] [--section=section-name] [--line-numbers]\n\
  121.        [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
  122.        [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
  123.        [--version] [--help] objfile...\n\
  124. at least one option besides -l (--line-numbers) must be given\n",
  125.        program_name);
  126.   exit (status);
  127. }
  128.  
  129. static struct option long_options[]=
  130. {
  131.   {"all-headers", no_argument, NULL, 'x'},
  132.   {"architecture", required_argument, NULL, 'm'},
  133.   {"archive-headers", no_argument, NULL, 'a'},
  134.   {"disassemble", no_argument, NULL, 'd'},
  135.   {"disassemble-all", no_argument, NULL, 'D'},
  136.   {"dynamic-reloc", no_argument, NULL, 'R'},
  137.   {"dynamic-syms", no_argument, NULL, 'T'},
  138.   {"file-headers", no_argument, NULL, 'f'},
  139.   {"full-contents", no_argument, NULL, 's'},
  140.   {"headers", no_argument, NULL, 'h'},
  141.   {"help", no_argument, NULL, 'H'},
  142.   {"info", no_argument, NULL, 'i'},
  143.   {"line-numbers", no_argument, NULL, 'l'},
  144.   {"reloc", no_argument, NULL, 'r'},
  145.   {"section", required_argument, NULL, 'j'},
  146.   {"section-headers", no_argument, NULL, 'h'},
  147.   {"stabs", no_argument, &dump_stab_section_info, 1},
  148.   {"syms", no_argument, NULL, 't'},
  149.   {"target", required_argument, NULL, 'b'},
  150.   {"version", no_argument, &show_version,    1},
  151.   {0, no_argument, 0, 0}
  152. };
  153.  
  154. static void
  155. dump_section_header (abfd, section, ignored)
  156.      bfd *abfd;
  157.      asection *section;
  158.      PTR ignored;
  159. {
  160.   char *comma = "";
  161.  
  162. #define PF(x,y) \
  163.   if (section->flags & x) {  printf("%s%s",comma,y); comma = ", "; }
  164.  
  165.  
  166.   printf ("SECTION %d [%s]\t: size %08x",
  167.       section->index,
  168.       section->name,
  169.       (unsigned) bfd_get_section_size_before_reloc (section));
  170.   printf (" vma ");
  171.   printf_vma (section->vma);
  172.   printf (" align 2**%u\n ",
  173.       section->alignment_power);
  174.   PF (SEC_ALLOC, "ALLOC");
  175.   PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
  176.   PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
  177.   PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
  178.   PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
  179.   PF (SEC_LOAD, "LOAD");
  180.   PF (SEC_RELOC, "RELOC");
  181. #ifdef SEC_BALIGN
  182.   PF (SEC_BALIGN, "BALIGN");
  183. #endif
  184.   PF (SEC_READONLY, "READONLY");
  185.   PF (SEC_CODE, "CODE");
  186.   PF (SEC_DATA, "DATA");
  187.   PF (SEC_ROM, "ROM");
  188.   PF (SEC_DEBUGGING, "DEBUGGING");
  189.   PF (SEC_NEVER_LOAD, "NEVER_LOAD");
  190.   printf ("\n");
  191. #undef PF
  192. }
  193.  
  194. static void
  195. dump_headers (abfd)
  196.      bfd *abfd;
  197. {
  198.   bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
  199. }
  200.  
  201. static asymbol **
  202. slurp_symtab (abfd)
  203.      bfd *abfd;
  204. {
  205.   asymbol **sy = (asymbol **) NULL;
  206.   long storage;
  207.  
  208.   if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
  209.     {
  210.       printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
  211.       return NULL;
  212.     }
  213.  
  214.   storage = bfd_get_symtab_upper_bound (abfd);
  215.   if (storage < 0)
  216.     bfd_fatal (bfd_get_filename (abfd));
  217.  
  218.   if (storage)
  219.     {
  220.       sy = (asymbol **) xmalloc (storage);
  221.     }
  222.   symcount = bfd_canonicalize_symtab (abfd, sy);
  223.   if (symcount < 0)
  224.     bfd_fatal (bfd_get_filename (abfd));
  225.   if (symcount == 0)
  226.     fprintf (stderr, "%s: %s: No symbols\n",
  227.          program_name, bfd_get_filename (abfd));
  228.   return sy;
  229. }
  230.  
  231. /* Read in the dynamic symbols.  */
  232.  
  233. static asymbol **
  234. slurp_dynamic_symtab (abfd)
  235.      bfd *abfd;
  236. {
  237.   asymbol **sy = (asymbol **) NULL;
  238.   long storage;
  239.  
  240.   storage = bfd_get_dynamic_symtab_upper_bound (abfd);
  241.   if (storage < 0)
  242.     {
  243.       if (!(bfd_get_file_flags (abfd) & DYNAMIC))
  244.     {
  245.       fprintf (stderr, "%s: %s: not a dynamic object\n",
  246.            program_name, bfd_get_filename (abfd));
  247.       return NULL;
  248.     }
  249.  
  250.       bfd_fatal (bfd_get_filename (abfd));
  251.     }
  252.  
  253.   if (storage)
  254.     {
  255.       sy = (asymbol **) xmalloc (storage);
  256.     }
  257.   dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
  258.   if (dynsymcount < 0)
  259.     bfd_fatal (bfd_get_filename (abfd));
  260.   if (dynsymcount == 0)
  261.     fprintf (stderr, "%s: %s: No dynamic symbols\n",
  262.          program_name, bfd_get_filename (abfd));
  263.   return sy;
  264. }
  265.  
  266. /* Filter out (in place) symbols that are useless for disassembly.
  267.    COUNT is the number of elements in SYMBOLS.
  268.    Return the number of useful symbols. */
  269.  
  270. long
  271. remove_useless_symbols (symbols, count)
  272.      asymbol **symbols;
  273.      long count;
  274. {
  275.   register asymbol **in_ptr = symbols, **out_ptr = symbols;
  276.  
  277.   while (--count >= 0)
  278.     {
  279.       asymbol *sym = *in_ptr++;
  280.  
  281.       if (sym->name == NULL || sym->name[0] == '\0')
  282.     continue;
  283.       if (sym->flags & (BSF_DEBUGGING))
  284.     continue;
  285.       if (bfd_is_und_section (sym->section)
  286.       || bfd_is_com_section (sym->section))
  287.     continue;
  288.  
  289.       *out_ptr++ = sym;
  290.     }
  291.   return out_ptr - symbols;
  292. }
  293.  
  294. /* Sort symbols into value order. */
  295.  
  296. static int 
  297. compare_symbols (ap, bp)
  298.      const PTR ap;
  299.      const PTR bp;
  300. {
  301.   const asymbol *a = *(const asymbol **)ap;
  302.   const asymbol *b = *(const asymbol **)bp;
  303.  
  304.   if (a->value > b->value)
  305.     return 1;
  306.   else if (a->value < b->value)
  307.     return -1;
  308.  
  309.   if (a->section > b->section)
  310.     return 1;
  311.   else if (a->section < b->section)
  312.     return -1;
  313.   return 0;
  314. }
  315.  
  316. /* Sort relocs into address order.  */
  317.  
  318. static int
  319. compare_relocs (ap, bp)
  320.      const PTR ap;
  321.      const PTR bp;
  322. {
  323.   const arelent *a = *(const arelent **)ap;
  324.   const arelent *b = *(const arelent **)bp;
  325.  
  326.   if (a->address > b->address)
  327.     return 1;
  328.   else if (a->address < b->address)
  329.     return -1;
  330.  
  331.   return compare_symbols ((const PTR) a->sym_ptr_ptr,
  332.               (const PTR) b->sym_ptr_ptr);
  333. }
  334.  
  335. /* Print VMA symbolically to INFO if possible.  */
  336.  
  337. static void
  338. objdump_print_address (vma, info)
  339.      bfd_vma vma;
  340.      struct disassemble_info *info;
  341. {
  342.   /* @@ For relocateable files, should filter out symbols belonging to
  343.      the wrong section.  Unfortunately, not enough information is supplied
  344.      to this routine to determine the correct section in all cases.  */
  345.   /* @@ Would it speed things up to cache the last two symbols returned,
  346.      and maybe their address ranges?  For many processors, only one memory
  347.      operand can be present at a time, so the 2-entry cache wouldn't be
  348.      constantly churned by code doing heavy memory accesses.  */
  349.  
  350.   /* Indices in `sorted_syms'.  */
  351.   long min = 0;
  352.   long max = sorted_symcount;
  353.   long thisplace;
  354.  
  355.   bfd_signed_vma vardiff;
  356.  
  357.   fprintf_vma (info->stream, vma);
  358.  
  359.   if (sorted_symcount < 1)
  360.     return;
  361.  
  362.   /* Perform a binary search looking for the closest symbol to the
  363.      required value.  We are searching the range (min, max].  */
  364.   while (min + 1 < max)
  365.     {
  366.       asymbol *sym;
  367.  
  368.       thisplace = (max + min) / 2;
  369.       sym = sorted_syms[thisplace];
  370.  
  371.       vardiff = sym->value - vma;
  372.  
  373.       if (vardiff > 0)
  374.     max = thisplace;
  375.       else if (vardiff < 0)
  376.     min = thisplace;
  377.       else
  378.     {
  379.       min = thisplace;
  380.       break;
  381.     }
  382.     }
  383.  
  384.   /* The symbol we want is now in min, the low end of the range we
  385.      were searching.  */
  386.   thisplace = min;
  387.  
  388.   {
  389.     /* If this symbol isn't global, search for one with the same value
  390.        that is.  */
  391.     bfd_vma val = sorted_syms[thisplace]->value;
  392.     long i;
  393.     if (sorted_syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
  394.       for (i = thisplace - 1; i >= 0; i--)
  395.     {
  396.       if (sorted_syms[i]->value == val
  397.           && (!(sorted_syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
  398.           || ((sorted_syms[thisplace]->flags & BSF_DEBUGGING)
  399.               && !(sorted_syms[i]->flags & BSF_DEBUGGING))))
  400.         {
  401.           thisplace = i;
  402.           break;
  403.         }
  404.     }
  405.     if (sorted_syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
  406.       for (i = thisplace + 1; i < sorted_symcount; i++)
  407.     {
  408.       if (sorted_syms[i]->value == val
  409.           && (!(sorted_syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
  410.           || ((sorted_syms[thisplace]->flags & BSF_DEBUGGING)
  411.               && !(sorted_syms[i]->flags & BSF_DEBUGGING))))
  412.         {
  413.           thisplace = i;
  414.           break;
  415.         }
  416.     }
  417.   }
  418.   {
  419.     /* If the file is relocateable, and the symbol could be from this
  420.        section, prefer a symbol from this section over symbols from
  421.        others, even if the other symbol's value might be closer.
  422.        
  423.        Note that this may be wrong for some symbol references if the
  424.        sections have overlapping memory ranges, but in that case there's
  425.        no way to tell what's desired without looking at the relocation
  426.        table.  */
  427.     struct objdump_disasm_info *aux;
  428.     long i;
  429.  
  430.     aux = (struct objdump_disasm_info *) info->application_data;
  431.     if (sorted_syms[thisplace]->section != aux->sec
  432.     && (aux->require_sec
  433.         || ((aux->abfd->flags & HAS_RELOC) != 0
  434.         && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
  435.         && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
  436.               + bfd_get_section_size_before_reloc (aux->sec)))))
  437.       {
  438.     for (i = thisplace + 1; i < sorted_symcount; i++)
  439.       {
  440.         if (sorted_syms[i]->value != sorted_syms[thisplace]->value)
  441.           break;
  442.       }
  443.     --i;
  444.     for (; i >= 0; i--)
  445.       {
  446.         if (sorted_syms[i]->section == aux->sec)
  447.           {
  448.         thisplace = i;
  449.         break;
  450.           }
  451.       }
  452.  
  453.     if (sorted_syms[thisplace]->section != aux->sec)
  454.       {
  455.         /* We didn't find a good symbol with a smaller value.
  456.                Look for one with a larger value.  */
  457.         for (i = thisplace + 1; i < sorted_symcount; i++)
  458.           {
  459.         if (sorted_syms[i]->section == aux->sec)
  460.           {
  461.             thisplace = i;
  462.             break;
  463.           }
  464.           }
  465.       }
  466.       }
  467.   }
  468.  
  469.   fprintf (info->stream, " <%s", sorted_syms[thisplace]->name);
  470.   if (sorted_syms[thisplace]->value > vma)
  471.     {
  472.       char buf[30], *p = buf;
  473.       sprintf_vma (buf, sorted_syms[thisplace]->value - vma);
  474.       while (*p == '0')
  475.     p++;
  476.       fprintf (info->stream, "-%s", p);
  477.     }
  478.   else if (vma > sorted_syms[thisplace]->value)
  479.     {
  480.       char buf[30], *p = buf;
  481.       sprintf_vma (buf, vma - sorted_syms[thisplace]->value);
  482.       while (*p == '0')
  483.     p++;
  484.       fprintf (info->stream, "+%s", p);
  485.     }
  486.   fprintf (info->stream, ">");
  487. }
  488.  
  489. void
  490. disassemble_data (abfd)
  491.      bfd *abfd;
  492. {
  493.   long i;
  494.   unsigned int (*print) () = 0; /* Old style */
  495.   disassembler_ftype disassemble_fn = 0; /* New style */
  496.   struct disassemble_info disasm_info;
  497.   struct objdump_disasm_info aux;
  498.  
  499.   int prevline = 0;
  500.   char *prev_function = NULL;
  501.  
  502.   asection *section;
  503.  
  504.   boolean done_dot = false;
  505.  
  506.   /* Replace symbol section relative values with abs values.  */
  507.   for (i = 0; i < symcount; i++)
  508.     {
  509.       syms[i]->value += syms[i]->section->vma;
  510.     }
  511.  
  512.   /* We make a copy of syms to sort.  We don't want to sort syms
  513.      because that will screw up the relocs.  */
  514.   sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
  515.   memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
  516.  
  517.   sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
  518.  
  519.   /* Sort the symbols into section and symbol order */
  520.   qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
  521.  
  522.   INIT_DISASSEMBLE_INFO(disasm_info, stdout);
  523.   disasm_info.application_data = (PTR) &aux;
  524.   aux.abfd = abfd;
  525.   disasm_info.print_address_func = objdump_print_address;
  526.  
  527.   if (machine != (char *) NULL)
  528.     {
  529.       bfd_arch_info_type *info = bfd_scan_arch (machine);
  530.       if (info == NULL)
  531.     {
  532.       fprintf (stderr, "%s: Can't use supplied machine %s\n",
  533.            program_name,
  534.            machine);
  535.       exit (1);
  536.     }
  537.       abfd->arch_info = info;
  538.     }
  539.  
  540.   /* See if we can disassemble using bfd.  */
  541.  
  542.   if (abfd->arch_info->disassemble)
  543.     {
  544.       print = abfd->arch_info->disassemble;
  545.     }
  546.   else
  547.     {
  548.       disassemble_fn = disassembler (abfd);
  549.       if (!disassemble_fn)
  550.     {
  551.       fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
  552.            program_name,
  553.            bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
  554.       exit (1);
  555.     }
  556.     }
  557.  
  558.   for (section = abfd->sections;
  559.        section != (asection *) NULL;
  560.        section = section->next)
  561.     {
  562.       bfd_byte *data = NULL;
  563.       bfd_size_type datasize = 0;
  564.       arelent **relbuf = NULL;
  565.       arelent **relpp = NULL;
  566.       arelent **relppend = NULL;
  567.  
  568.       if ((section->flags & SEC_LOAD) == 0
  569.       || (! disassemble_all
  570.           && only == NULL
  571.           && (section->flags & SEC_CODE) == 0))
  572.     continue;
  573.       if (only != (char *) NULL && strcmp (only, section->name) != 0)
  574.     continue;
  575.  
  576.       if (dump_reloc_info
  577.       && (section->flags & SEC_RELOC) != 0)
  578.     {
  579.       long relsize;
  580.  
  581.       relsize = bfd_get_reloc_upper_bound (abfd, section);
  582.       if (relsize < 0)
  583.         bfd_fatal (bfd_get_filename (abfd));
  584.  
  585.       if (relsize > 0)
  586.         {
  587.           long relcount;
  588.  
  589.           relbuf = (arelent **) xmalloc (relsize);
  590.           relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
  591.           if (relcount < 0)
  592.         bfd_fatal (bfd_get_filename (abfd));
  593.  
  594.           /* Sort the relocs by address.  */
  595.           qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
  596.  
  597.           relpp = relbuf;
  598.           relppend = relpp + relcount;
  599.         }
  600.     }
  601.  
  602.       printf ("Disassembly of section %s:\n", section->name);
  603.  
  604.       datasize = bfd_get_section_size_before_reloc (section);
  605.       if (datasize == 0)
  606.     continue;
  607.  
  608.       data = (bfd_byte *) xmalloc ((size_t) datasize);
  609.  
  610.       bfd_get_section_contents (abfd, section, data, 0, datasize);
  611.  
  612.       aux.sec = section;
  613.       disasm_info.buffer = data;
  614.       disasm_info.buffer_vma = section->vma;
  615.       disasm_info.buffer_length = datasize;
  616.       i = 0;
  617.       while (i < disasm_info.buffer_length)
  618.     {
  619.       int bytes;
  620.  
  621.       if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
  622.           data[i + 3] == 0)
  623.         {
  624.           if (done_dot == false)
  625.         {
  626.           printf ("...\n");
  627.           done_dot = true;
  628.         }
  629.           bytes = 4;
  630.         }
  631.       else
  632.         {
  633.           done_dot = false;
  634.           if (with_line_numbers)
  635.         {
  636.           CONST char *filename;
  637.           CONST char *functionname;
  638.           unsigned int line;
  639.  
  640.           if (bfd_find_nearest_line (abfd,
  641.                          section,
  642.                          syms,
  643.                          section->vma + i,
  644.                          &filename,
  645.                          &functionname,
  646.                          &line))
  647.             {
  648.               if (functionname
  649.               && *functionname != '\0'
  650.               && (prev_function == NULL
  651.                   || strcmp (functionname, prev_function) != 0))
  652.             {
  653.               printf ("%s():\n", functionname);
  654.               if (prev_function != NULL)
  655.                 free (prev_function);
  656.               prev_function = xmalloc (strlen (functionname) + 1);
  657.               strcpy (prev_function, functionname);
  658.             }
  659.               if (!filename)
  660.             filename = "???";
  661.               if (line && line != prevline)
  662.             {
  663.               printf ("%s:%u\n", filename, line);
  664.               prevline = line;
  665.             }
  666.             }
  667.         }
  668.           aux.require_sec = true;
  669.           objdump_print_address (section->vma + i, &disasm_info);
  670.           aux.require_sec = false;
  671.           putchar (' ');
  672.  
  673.           if (disassemble_fn)
  674.         {
  675.           /* New style */
  676.           bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
  677.           if (bytes < 0)
  678.             break;
  679.         }
  680.           else
  681.         {
  682.           /* Old style */
  683.           bytes = print (section->vma + i, data + i, stdout);
  684.         }
  685.           putchar ('\n');
  686.         }
  687.  
  688.       if (dump_reloc_info
  689.           && (section->flags & SEC_RELOC) != 0)
  690.         {
  691.           while (relpp < relppend
  692.              && ((*relpp)->address >= i
  693.              && (*relpp)->address < i + bytes))
  694.         {
  695.           arelent *q;
  696.           const char *sym_name;
  697.  
  698.           q = *relpp;
  699.  
  700.           printf ("\t\tRELOC: ");
  701.  
  702.           printf_vma (section->vma + q->address);
  703.  
  704.           printf (" %s ", q->howto->name);
  705.  
  706.           if (q->sym_ptr_ptr != NULL
  707.               && *q->sym_ptr_ptr != NULL)
  708.             {
  709.               sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
  710.               if (sym_name == NULL || *sym_name == '\0')
  711.             {
  712.               asection *sym_sec;
  713.  
  714.               sym_sec = bfd_get_section (*q->sym_ptr_ptr);
  715.               sym_name = bfd_get_section_name (abfd, sym_sec);
  716.               if (sym_name == NULL || *sym_name == '\0')
  717.                 sym_name = "*unknown*";
  718.             }
  719.             }
  720.  
  721.           printf ("%s", sym_name);
  722.  
  723.           if (q->addend)
  724.             {
  725.               printf ("+0x");
  726.               printf_vma (q->addend);
  727.             }
  728.  
  729.           printf ("\n");
  730.  
  731.           ++relpp;
  732.         }
  733.         }
  734.  
  735.       i += bytes;
  736.     }
  737.  
  738.       free (data);
  739.       if (relbuf != NULL)
  740.     free (relbuf);
  741.     }
  742. }
  743.  
  744.  
  745. /* Define a table of stab values and print-strings.  We wish the initializer
  746.    could be a direct-mapped table, but instead we build one the first
  747.    time we need it.  */
  748.  
  749. char **stab_name;
  750.  
  751. struct stab_print {
  752.   int value;
  753.   char *string;
  754. };
  755.  
  756. struct stab_print stab_print[] = {
  757. #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
  758. #include "aout/stab.def"
  759. #undef __define_stab
  760.   {0, ""}
  761. };
  762.  
  763. void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
  764.                  char *strsect_name));
  765.  
  766. /* Dump the stabs sections from an object file that has a section that
  767.    uses Sun stabs encoding.  It has to use some hooks into BFD because
  768.    string table sections are not normally visible to BFD callers.  */
  769.  
  770. void
  771. dump_stabs (abfd)
  772.      bfd *abfd;
  773. {
  774.   /* Allocate and initialize stab name array if first time.  */
  775.   if (stab_name == NULL) 
  776.     {
  777.       int i;
  778.  
  779.       stab_name = (char **) xmalloc (256 * sizeof(char *));
  780.       /* Clear the array. */
  781.       for (i = 0; i < 256; i++)
  782.     stab_name[i] = NULL;
  783.       /* Fill in the defined stabs. */
  784.       for (i = 0; *stab_print[i].string; i++)
  785.     stab_name[stab_print[i].value] = stab_print[i].string;
  786.     }
  787.  
  788.   dump_section_stabs (abfd, ".stab", ".stabstr");
  789.   dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
  790.   dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
  791.   dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
  792. }
  793.  
  794. static struct internal_nlist *stabs;
  795. static bfd_size_type stab_size;
  796.  
  797. static char *strtab;
  798. static bfd_size_type stabstr_size;
  799.  
  800. /* Read ABFD's stabs section STABSECT_NAME into `stabs'
  801.    and string table section STRSECT_NAME into `strtab'.
  802.    If the section exists and was read, allocate the space and return true.
  803.    Otherwise return false.  */
  804.  
  805. boolean
  806. read_section_stabs (abfd, stabsect_name, strsect_name)
  807.      bfd *abfd;
  808.      char *stabsect_name;
  809.      char *strsect_name;
  810. {
  811.   asection *stabsect, *stabstrsect;
  812.  
  813.   stabsect = bfd_get_section_by_name (abfd, stabsect_name);
  814.   if (0 == stabsect)
  815.     {
  816.       printf ("No %s section present\n\n", stabsect_name);
  817.       return false;
  818.     }
  819.  
  820.   stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
  821.   if (0 == stabstrsect)
  822.     {
  823.       fprintf (stderr, "%s: %s has no %s section\n", program_name,
  824.            bfd_get_filename (abfd), strsect_name);
  825.       return false;
  826.     }
  827.  
  828.   stab_size    = bfd_section_size (abfd, stabsect);
  829.   stabstr_size = bfd_section_size (abfd, stabstrsect);
  830.  
  831.   stabs  = (struct internal_nlist *) xmalloc (stab_size);
  832.   strtab = (char *) xmalloc (stabstr_size);
  833.   
  834.   if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
  835.     {
  836.       fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
  837.            program_name, stabsect_name, bfd_get_filename (abfd),
  838.            bfd_errmsg (bfd_get_error ()));
  839.       free (stabs);
  840.       free (strtab);
  841.       return false;
  842.     }
  843.  
  844.   if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
  845.                   stabstr_size))
  846.     {
  847.       fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
  848.            program_name, strsect_name, bfd_get_filename (abfd),
  849.            bfd_errmsg (bfd_get_error ()));
  850.       free (stabs);
  851.       free (strtab);
  852.       return false;
  853.     }
  854.  
  855.   return true;
  856. }
  857.  
  858. #define SWAP_SYMBOL(symp, abfd) \
  859. { \
  860.     (symp)->n_strx = bfd_h_get_32(abfd,            \
  861.                   (unsigned char *)&(symp)->n_strx);    \
  862.     (symp)->n_desc = bfd_h_get_16 (abfd,            \
  863.                    (unsigned char *)&(symp)->n_desc);      \
  864.     (symp)->n_value = bfd_h_get_32 (abfd,            \
  865.                     (unsigned char *)&(symp)->n_value);    \
  866. }
  867.  
  868. /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
  869.    using string table section STRSECT_NAME (in `strtab').  */
  870.  
  871. void
  872. print_section_stabs (abfd, stabsect_name, strsect_name)
  873.      bfd *abfd;
  874.      char *stabsect_name;
  875.      char *strsect_name;
  876. {
  877.   int i;
  878.   unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
  879.   struct internal_nlist *stabp = stabs,
  880.   *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs);
  881.  
  882.   printf ("Contents of %s section:\n\n", stabsect_name);
  883.   printf ("Symnum n_type n_othr n_desc n_value  n_strx String\n");
  884.  
  885.   /* Loop through all symbols and print them.
  886.  
  887.      We start the index at -1 because there is a dummy symbol on
  888.      the front of stabs-in-{coff,elf} sections that supplies sizes.  */
  889.  
  890.   for (i = -1; stabp < stabs_end; stabp++, i++)
  891.     {
  892.       SWAP_SYMBOL (stabp, abfd);
  893.       printf ("\n%-6d ", i);
  894.       /* Either print the stab name, or, if unnamed, print its number
  895.      again (makes consistent formatting for tools like awk). */
  896.       if (stab_name[stabp->n_type])
  897.     printf ("%-6s", stab_name[stabp->n_type]);
  898.       else if (stabp->n_type == N_UNDF)
  899.     printf ("HdrSym");
  900.       else
  901.     printf ("%-6d", stabp->n_type);
  902.       printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc);
  903.       printf_vma (stabp->n_value);
  904.       printf (" %-6lu", stabp->n_strx);
  905.  
  906.       /* Symbols with type == 0 (N_UNDF) specify the length of the
  907.      string table associated with this file.  We use that info
  908.      to know how to relocate the *next* file's string table indices.  */
  909.  
  910.       if (stabp->n_type == N_UNDF)
  911.     {
  912.       file_string_table_offset = next_file_string_table_offset;
  913.       next_file_string_table_offset += stabp->n_value;
  914.     }
  915.       else
  916.     {
  917.       /* Using the (possibly updated) string table offset, print the
  918.          string (if any) associated with this symbol.  */
  919.  
  920.       if ((stabp->n_strx + file_string_table_offset) < stabstr_size)
  921.         printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]);
  922.       else
  923.         printf (" *");
  924.     }
  925.     }
  926.   printf ("\n\n");
  927. }
  928.  
  929. void
  930. dump_section_stabs (abfd, stabsect_name, strsect_name)
  931.      bfd *abfd;
  932.      char *stabsect_name;
  933.      char *strsect_name;
  934. {
  935.   if (read_section_stabs (abfd, stabsect_name, strsect_name))
  936.     {
  937.       print_section_stabs (abfd, stabsect_name, strsect_name);
  938.       free (stabs);
  939.       free (strtab);
  940.     }
  941. }
  942.  
  943. static void
  944. dump_bfd_header (abfd)
  945.      bfd *abfd;
  946. {
  947.   char *comma = "";
  948.  
  949.   printf ("architecture: %s, ",
  950.       bfd_printable_arch_mach (bfd_get_arch (abfd),
  951.                    bfd_get_mach (abfd)));
  952.   printf ("flags 0x%08x:\n", abfd->flags);
  953.  
  954. #define PF(x, y)    if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
  955.   PF (HAS_RELOC, "HAS_RELOC");
  956.   PF (EXEC_P, "EXEC_P");
  957.   PF (HAS_LINENO, "HAS_LINENO");
  958.   PF (HAS_DEBUG, "HAS_DEBUG");
  959.   PF (HAS_SYMS, "HAS_SYMS");
  960.   PF (HAS_LOCALS, "HAS_LOCALS");
  961.   PF (DYNAMIC, "DYNAMIC");
  962.   PF (WP_TEXT, "WP_TEXT");
  963.   PF (D_PAGED, "D_PAGED");
  964.   PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
  965.   printf ("\nstart address 0x");
  966.   printf_vma (abfd->start_address);
  967. }
  968.  
  969. static void
  970. display_bfd (abfd)
  971.      bfd *abfd;
  972. {
  973.   char **matching;
  974.  
  975.   if (!bfd_check_format_matches (abfd, bfd_object, &matching))
  976.     {
  977.       bfd_nonfatal (bfd_get_filename (abfd));
  978.       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  979.     {
  980.       list_matching_formats (matching);
  981.       free (matching);
  982.     }
  983.       return;
  984.     }
  985.  
  986.   printf ("\n%s:     file format %s\n", bfd_get_filename (abfd),
  987.       abfd->xvec->name);
  988.   if (dump_ar_hdrs)
  989.     print_arelt_descr (stdout, abfd, true);
  990.   if (dump_file_header)
  991.     dump_bfd_header (abfd);
  992.   putchar ('\n');
  993.   if (dump_section_headers)
  994.     dump_headers (abfd);
  995.   if (dump_symtab || dump_reloc_info || disassemble)
  996.     {
  997.       syms = slurp_symtab (abfd);
  998.     }
  999.   if (dump_dynamic_symtab || dump_dynamic_reloc_info)
  1000.     {
  1001.       dynsyms = slurp_dynamic_symtab (abfd);
  1002.     }
  1003.   if (dump_symtab)
  1004.     dump_symbols (abfd, false);
  1005.   if (dump_dynamic_symtab)
  1006.     dump_symbols (abfd, true);
  1007.   if (dump_stab_section_info)
  1008.     dump_stabs (abfd);
  1009.   if (dump_reloc_info && ! disassemble)
  1010.     dump_relocs (abfd);
  1011.   if (dump_dynamic_reloc_info)
  1012.     dump_dynamic_relocs (abfd);
  1013.   if (dump_section_contents)
  1014.     dump_data (abfd);
  1015.   if (disassemble)
  1016.     disassemble_data (abfd);
  1017. }
  1018.  
  1019. static void
  1020. display_file (filename, target)
  1021.      char *filename;
  1022.      char *target;
  1023. {
  1024.   bfd *file, *arfile = (bfd *) NULL;
  1025.  
  1026.   file = bfd_openr (filename, target);
  1027.   if (file == NULL)
  1028.     {
  1029.       bfd_nonfatal (filename);
  1030.       return;
  1031.     }
  1032.  
  1033.   if (bfd_check_format (file, bfd_archive) == true)
  1034.     {
  1035.       bfd *last_arfile = NULL;
  1036.  
  1037.       printf ("In archive %s:\n", bfd_get_filename (file));
  1038.       for (;;)
  1039.     {
  1040.       bfd_set_error (bfd_error_no_error);
  1041.  
  1042.       arfile = bfd_openr_next_archived_file (file, arfile);
  1043.       if (arfile == NULL)
  1044.         {
  1045.           if (bfd_get_error () != bfd_error_no_more_archived_files)
  1046.         {
  1047.           bfd_nonfatal (bfd_get_filename (file));
  1048.         }
  1049.           break;
  1050.         }
  1051.  
  1052.       display_bfd (arfile);
  1053.  
  1054.       if (last_arfile != NULL)
  1055.         bfd_close (last_arfile);
  1056.       last_arfile = arfile;
  1057.     }
  1058.  
  1059.       if (last_arfile != NULL)
  1060.     bfd_close (last_arfile);
  1061.     }
  1062.   else
  1063.     display_bfd (file);
  1064.  
  1065.   bfd_close (file);
  1066. }
  1067.  
  1068. /* Actually display the various requested regions */
  1069.  
  1070. static void
  1071. dump_data (abfd)
  1072.      bfd *abfd;
  1073. {
  1074.   asection *section;
  1075.   bfd_byte *data = 0;
  1076.   bfd_size_type datasize = 0;
  1077.   bfd_size_type i;
  1078.  
  1079.   for (section = abfd->sections; section != NULL; section =
  1080.        section->next)
  1081.     {
  1082.       int onaline = 16;
  1083.  
  1084.       if (only == (char *) NULL ||
  1085.       strcmp (only, section->name) == 0)
  1086.     {
  1087.       if (section->flags & SEC_HAS_CONTENTS)
  1088.         {
  1089.           printf ("Contents of section %s:\n", section->name);
  1090.  
  1091.           if (bfd_section_size (abfd, section) == 0)
  1092.         continue;
  1093.           data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
  1094.           datasize = bfd_section_size (abfd, section);
  1095.  
  1096.  
  1097.           bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
  1098.  
  1099.           for (i = 0; i < bfd_section_size (abfd, section); i += onaline)
  1100.         {
  1101.           bfd_size_type j;
  1102.  
  1103.           printf (" %04lx ", (unsigned long int) (i + section->vma));
  1104.           for (j = i; j < i + onaline; j++)
  1105.             {
  1106.               if (j < bfd_section_size (abfd, section))
  1107.             printf ("%02x", (unsigned) (data[j]));
  1108.               else
  1109.             printf ("  ");
  1110.               if ((j & 3) == 3)
  1111.             printf (" ");
  1112.             }
  1113.  
  1114.           printf (" ");
  1115.           for (j = i; j < i + onaline; j++)
  1116.             {
  1117.               if (j >= bfd_section_size (abfd, section))
  1118.             printf (" ");
  1119.               else
  1120.             printf ("%c", isprint (data[j]) ? data[j] : '.');
  1121.             }
  1122.           putchar ('\n');
  1123.         }
  1124.           free (data);
  1125.         }
  1126.     }
  1127.     }
  1128. }
  1129.  
  1130. /* Should perhaps share code and display with nm? */
  1131. static void
  1132. dump_symbols (abfd, dynamic)
  1133.      bfd *abfd;
  1134.      boolean dynamic;
  1135. {
  1136.   asymbol **current;
  1137.   long max;
  1138.   long count;
  1139.  
  1140.   if (dynamic)
  1141.     {
  1142.       current = dynsyms;
  1143.       max = dynsymcount;
  1144.       if (max == 0)
  1145.     return;
  1146.       printf ("DYNAMIC SYMBOL TABLE:\n");
  1147.     }
  1148.   else
  1149.     {
  1150.       current = syms;
  1151.       max = symcount;
  1152.       if (max == 0)
  1153.     return;
  1154.       printf ("SYMBOL TABLE:\n");
  1155.     }
  1156.  
  1157.   for (count = 0; count < max; count++)
  1158.     {
  1159.       if (*current)
  1160.     {
  1161.       bfd *cur_bfd = bfd_asymbol_bfd(*current);
  1162.       if (cur_bfd)
  1163.         {
  1164.           bfd_print_symbol (cur_bfd,
  1165.                 stdout,
  1166.                 *current, bfd_print_symbol_all);
  1167.           printf ("\n");
  1168.         }
  1169.     }
  1170.       current++;
  1171.     }
  1172.   printf ("\n");
  1173.   printf ("\n");
  1174. }
  1175.  
  1176. static void
  1177. dump_relocs (abfd)
  1178.      bfd *abfd;
  1179. {
  1180.   arelent **relpp;
  1181.   long relcount;
  1182.   asection *a;
  1183.  
  1184.   for (a = abfd->sections; a != (asection *) NULL; a = a->next)
  1185.     {
  1186.       long relsize;
  1187.  
  1188.       if (bfd_is_abs_section (a))
  1189.     continue;
  1190.       if (bfd_is_und_section (a))
  1191.     continue;
  1192.       if (bfd_is_com_section (a))
  1193.     continue;
  1194.  
  1195.       if (only)
  1196.     {
  1197.       if (strcmp (only, a->name))
  1198.         continue;
  1199.     }
  1200.       else if ((a->flags & SEC_RELOC) == 0)
  1201.     continue;
  1202.  
  1203.       printf ("RELOCATION RECORDS FOR [%s]:", a->name);
  1204.  
  1205.       relsize = bfd_get_reloc_upper_bound (abfd, a);
  1206.       if (relsize < 0)
  1207.     bfd_fatal (bfd_get_filename (abfd));
  1208.  
  1209.       if (relsize == 0)
  1210.     {
  1211.       printf (" (none)\n\n");
  1212.     }
  1213.       else
  1214.     {
  1215.       relpp = (arelent **) xmalloc (relsize);
  1216.       relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
  1217.       if (relcount < 0)
  1218.         bfd_fatal (bfd_get_filename (abfd));
  1219.       else if (relcount == 0)
  1220.         {
  1221.           printf (" (none)\n\n");
  1222.         }
  1223.       else
  1224.         {
  1225.           printf ("\n");
  1226.           dump_reloc_set (abfd, relpp, relcount);
  1227.           printf ("\n\n");
  1228.         }
  1229.       free (relpp);
  1230.     }
  1231.     }
  1232. }
  1233.  
  1234. static void
  1235. dump_dynamic_relocs (abfd)
  1236.      bfd *abfd;
  1237. {
  1238.   long relsize;
  1239.   arelent **relpp;
  1240.   long relcount;
  1241.  
  1242.   printf ("DYNAMIC RELOCATION RECORDS");
  1243.  
  1244.   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
  1245.   if (relsize < 0)
  1246.     bfd_fatal (bfd_get_filename (abfd));
  1247.  
  1248.   if (relsize == 0)
  1249.     {
  1250.       printf (" (none)\n\n");
  1251.     }
  1252.   else
  1253.     {
  1254.       relpp = (arelent **) xmalloc (relsize);
  1255.       relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
  1256.       if (relcount < 0)
  1257.     bfd_fatal (bfd_get_filename (abfd));
  1258.       else if (relcount == 0)
  1259.     {
  1260.       printf (" (none)\n\n");
  1261.     }
  1262.       else
  1263.     {
  1264.       printf ("\n");
  1265.       dump_reloc_set (abfd, relpp, relcount);
  1266.       printf ("\n\n");
  1267.     }
  1268.       free (relpp);
  1269.     }
  1270. }
  1271.  
  1272. static void
  1273. dump_reloc_set (abfd, relpp, relcount)
  1274.      bfd *abfd;
  1275.      arelent **relpp;
  1276.      long relcount;
  1277. {
  1278.   arelent **p;
  1279.  
  1280.   /* Get column headers lined up reasonably.  */
  1281.   {
  1282.     static int width;
  1283.     if (width == 0)
  1284.       {
  1285.     char buf[30];
  1286.     sprintf_vma (buf, (bfd_vma) -1);
  1287.     width = strlen (buf) - 7;
  1288.       }
  1289.     printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
  1290.   }
  1291.  
  1292.   for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
  1293.     {
  1294.       arelent *q = *p;
  1295.       CONST char *sym_name;
  1296.       CONST char *section_name;
  1297.  
  1298.       if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
  1299.     {
  1300.       sym_name = (*(q->sym_ptr_ptr))->name;
  1301.       section_name = (*(q->sym_ptr_ptr))->section->name;
  1302.     }
  1303.       else
  1304.     {
  1305.       sym_name = NULL;
  1306.       section_name = NULL;
  1307.     }
  1308.       if (sym_name)
  1309.     {
  1310.       printf_vma (q->address);
  1311.       printf (" %-16s  %s",
  1312.           q->howto->name,
  1313.           sym_name);
  1314.     }
  1315.       else
  1316.     {
  1317.       if (section_name == (CONST char *) NULL)
  1318.         section_name = "*unknown*";
  1319.       printf_vma (q->address);
  1320.       printf (" %-16s  [%s]",
  1321.           q->howto->name,
  1322.           section_name);
  1323.     }
  1324.       if (q->addend)
  1325.     {
  1326.       printf ("+0x");
  1327.       printf_vma (q->addend);
  1328.     }
  1329.       printf ("\n");
  1330.     }
  1331. }
  1332.  
  1333. /* The length of the longest architecture name + 1.  */
  1334. #define LONGEST_ARCH sizeof("rs6000:6000")
  1335.  
  1336. /* List the targets that BFD is configured to support, each followed
  1337.    by its endianness and the architectures it supports.  */
  1338.  
  1339. static void
  1340. display_target_list ()
  1341. {
  1342.   extern char *tmpnam ();
  1343.   extern bfd_target *bfd_target_vector[];
  1344.   char *dummy_name;
  1345.   int t;
  1346.  
  1347.   dummy_name = tmpnam ((char *) NULL);
  1348.   for (t = 0; bfd_target_vector[t]; t++)
  1349.     {
  1350.       bfd_target *p = bfd_target_vector[t];
  1351.       bfd *abfd = bfd_openw (dummy_name, p->name);
  1352.       int a;
  1353.  
  1354.       printf ("%s\n (header %s, data %s)\n", p->name,
  1355.           p->header_byteorder_big_p ? "big endian" : "little endian",
  1356.           p->byteorder_big_p ? "big endian" : "little endian");
  1357.  
  1358.       if (abfd == NULL)
  1359.     {
  1360.       bfd_nonfatal (dummy_name);
  1361.       continue;
  1362.     }
  1363.  
  1364.       if (! bfd_set_format (abfd, bfd_object))
  1365.     {
  1366.       if (bfd_get_error () != bfd_error_invalid_operation)
  1367.         bfd_nonfatal (p->name);
  1368.       continue;
  1369.     }
  1370.  
  1371.       for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
  1372.     if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
  1373.       printf ("  %s\n",
  1374.           bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
  1375.     }
  1376.   unlink (dummy_name);
  1377. }
  1378.  
  1379. /* Print a table showing which architectures are supported for entries
  1380.    FIRST through LAST-1 of bfd_target_vector (targets across,
  1381.    architectures down).  */
  1382.  
  1383. static void
  1384. display_info_table (first, last)
  1385.      int first;
  1386.      int last;
  1387. {
  1388.   extern bfd_target *bfd_target_vector[];
  1389.   extern char *tmpnam ();
  1390.   int t, a;
  1391.   char *dummy_name;
  1392.  
  1393.   /* Print heading of target names.  */
  1394.   printf ("\n%*s", (int) LONGEST_ARCH, " ");
  1395.   for (t = first; t < last && bfd_target_vector[t]; t++)
  1396.     printf ("%s ", bfd_target_vector[t]->name);
  1397.   putchar ('\n');
  1398.  
  1399.   dummy_name = tmpnam ((char *) NULL);
  1400.   for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
  1401.     if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
  1402.       {
  1403.     printf ("%*s ", (int) LONGEST_ARCH - 1,
  1404.         bfd_printable_arch_mach (a, 0));
  1405.     for (t = first; t < last && bfd_target_vector[t]; t++)
  1406.       {
  1407.         bfd_target *p = bfd_target_vector[t];
  1408.         boolean ok = true;
  1409.         bfd *abfd = bfd_openw (dummy_name, p->name);
  1410.  
  1411.         if (abfd == NULL)
  1412.           {
  1413.         bfd_nonfatal (p->name);
  1414.         ok = false;
  1415.           }
  1416.  
  1417.         if (ok)
  1418.           {
  1419.         if (! bfd_set_format (abfd, bfd_object))
  1420.           {
  1421.             if (bfd_get_error () != bfd_error_invalid_operation)
  1422.               bfd_nonfatal (p->name);
  1423.             ok = false;
  1424.           }
  1425.           }
  1426.  
  1427.         if (ok)
  1428.           {
  1429.         if (! bfd_set_arch_mach (abfd, a, 0))
  1430.           ok = false;
  1431.           }
  1432.  
  1433.         if (ok)
  1434.           printf ("%s ", p->name);
  1435.         else
  1436.           {
  1437.         int l = strlen (p->name);
  1438.         while (l--)
  1439.           putchar ('-');
  1440.         putchar (' ');
  1441.           }
  1442.       }
  1443.     putchar ('\n');
  1444.       }
  1445.   unlink (dummy_name);
  1446. }
  1447.  
  1448. /* Print tables of all the target-architecture combinations that
  1449.    BFD has been configured to support.  */
  1450.  
  1451. static void
  1452. display_target_tables ()
  1453. {
  1454.   int t, columns;
  1455.   extern bfd_target *bfd_target_vector[];
  1456.   char *colum;
  1457.   extern char *getenv ();
  1458.  
  1459.   columns = 0;
  1460.   colum = getenv ("COLUMNS");
  1461.   if (colum != NULL)
  1462.     columns = atoi (colum);
  1463.   if (columns == 0)
  1464.     columns = 80;
  1465.  
  1466.   t = 0;
  1467.   while (bfd_target_vector[t] != NULL)
  1468.     {
  1469.       int oldt = t, wid;
  1470.  
  1471.       wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
  1472.       ++t;
  1473.       while (wid < columns && bfd_target_vector[t] != NULL)
  1474.     {
  1475.       int newwid;
  1476.  
  1477.       newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
  1478.       if (newwid >= columns)
  1479.         break;
  1480.       wid = newwid;
  1481.       ++t;
  1482.     }
  1483.       display_info_table (oldt, t);
  1484.     }
  1485. }
  1486.  
  1487. static void
  1488. display_info ()
  1489. {
  1490.   printf ("BFD header file version %s\n", BFD_VERSION);
  1491.   display_target_list ();
  1492.   display_target_tables ();
  1493. }
  1494.  
  1495. int
  1496. main (argc, argv)
  1497.      int argc;
  1498.      char **argv;
  1499. {
  1500.   int c;
  1501.   char *target = default_target;
  1502.   boolean seenflag = false;
  1503.  
  1504.   program_name = *argv;
  1505.   xmalloc_set_program_name (program_name);
  1506.  
  1507.   bfd_init ();
  1508.  
  1509.   while ((c = getopt_long (argc, argv, "ib:m:VdDlfahrRtTxsj:", long_options,
  1510.                (int *) 0))
  1511.      != EOF)
  1512.     {
  1513.       seenflag = true;
  1514.       switch (c)
  1515.     {
  1516.     case 0:
  1517.       break;        /* we've been given a long option */
  1518.     case 'm':
  1519.       machine = optarg;
  1520.       break;
  1521.     case 'j':
  1522.       only = optarg;
  1523.       break;
  1524.     case 'l':
  1525.       with_line_numbers = 1;
  1526.       break;
  1527.     case 'b':
  1528.       target = optarg;
  1529.       break;
  1530.     case 'f':
  1531.       dump_file_header = true;
  1532.       break;
  1533.     case 'i':
  1534.       formats_info = true;
  1535.       break;
  1536.     case 'x':
  1537.       dump_symtab = 1;
  1538.       dump_reloc_info = 1;
  1539.       dump_file_header = true;
  1540.       dump_ar_hdrs = 1;
  1541.       dump_section_headers = 1;
  1542.       break;
  1543.     case 't':
  1544.       dump_symtab = 1;
  1545.       break;
  1546.     case 'T':
  1547.       dump_dynamic_symtab = 1;
  1548.       break;
  1549.     case 'd':
  1550.       disassemble = true;
  1551.       break;
  1552.     case 'D':
  1553.       disassemble = disassemble_all = true;
  1554.       break;
  1555.     case 's':
  1556.       dump_section_contents = 1;
  1557.       break;
  1558.     case 'r':
  1559.       dump_reloc_info = 1;
  1560.       break;
  1561.     case 'R':
  1562.       dump_dynamic_reloc_info = 1;
  1563.       break;
  1564.     case 'a':
  1565.       dump_ar_hdrs = 1;
  1566.       break;
  1567.     case 'h':
  1568.       dump_section_headers = 1;
  1569.       break;
  1570.     case 'H':
  1571.       usage (stdout, 0);
  1572.     case 'V':
  1573.       show_version = 1;
  1574.       break;
  1575.     default:
  1576.       usage (stderr, 1);
  1577.     }
  1578.     }
  1579.  
  1580.   if (show_version)
  1581.     {
  1582.       printf ("GNU %s version %s\n", program_name, program_version);
  1583.       exit (0);
  1584.     }
  1585.  
  1586.   if (seenflag == false)
  1587.     usage (stderr, 1);
  1588.  
  1589.   if (formats_info)
  1590.     {
  1591.       display_info ();
  1592.     }
  1593.   else
  1594.     {
  1595.       if (optind == argc)
  1596.     display_file ("a.out", target);
  1597.       else
  1598.     for (; optind < argc;)
  1599.       display_file (argv[optind++], target);
  1600.     }
  1601.   return 0;
  1602. }
  1603.